package com.jckj.core.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.dao.CmsArticleMapper;
import com.jckj.core.db.DataSourceEnum;
import com.jckj.core.entity.ArticalAttachmentKey;
import com.jckj.core.entity.CmsArticle;
import com.jckj.core.entity.CmsArticleExample;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.ArticalAttachmentService;
import com.jckj.core.service.CmsArticleService;
import com.jckj.core.util.StringUtils;
import com.jckj.core.vos.LoginUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;


/**
 *  <p>
 *    内容服务实现类
 * </p>
 * @author:liukuan
 * @Date:2018-01-26
 *
 *
 */
@Service
@Transactional(readOnly = true,rollbackFor = ServiceException.class)
public class CmsArticleServiceImpl implements CmsArticleService {

    private static Logger LOGGER = LoggerFactory.getLogger(CmsArticleServiceImpl.class);

    @Autowired
    private CmsArticleMapper cmsArticleMapper;
    @Autowired
    private ArticalAttachmentService articalAttachmentService;

    /**
     *
     * 根据栏目categoryId查询相关内容
     * @param pageNum
     * @param pageSize
     * @return
     */
    @CurrentDataSource(DataSourceEnum.SLAVE)
    @Override
    public PageInfo<CmsArticle> findByCategoryId(String categoryId,Integer pageNum,Integer pageSize) {
        CmsArticleExample example=new CmsArticleExample();
        CmsArticleExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        //这段代码表示，程序开始分页了，pageNum默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录
        PageHelper.startPage(pageNum,pageSize);
        List<CmsArticle> cmsArticleList = cmsArticleMapper.selectByExample(example);
        PageInfo<CmsArticle> p=new PageInfo<CmsArticle>(cmsArticleList);
        return  p;
    }

    /**
     * 添加内容
     *
     * @param cmsArticle
     * @return
     */

    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    @Override
    public int insert(String attachmentId,CmsArticle cmsArticle) {
        int count=0;
        try {
            int num= cmsArticleMapper.insert(cmsArticle);
            if(num>0){
                ArticalAttachmentKey ak=new ArticalAttachmentKey();
                ak.setArticalId(cmsArticle.getId());
                ak.setAttachementId(attachmentId);
               count= articalAttachmentService.insert(ak);
            }
        } catch (Exception e) {
            LOGGER.error("保存内容信息 %s:",e);
            throw new ServiceException("保存内容出错",e);
        }
        return count;
    }

    /**
     * 修改内容
     * attachmentId:是附件表的主键
     * @param cmsArticle
     * @return
     */
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    @Override
    public int update(String attachmentId,CmsArticle cmsArticle) {
        int num=0;
        try {
            num = cmsArticleMapper.updateByPrimaryKeySelective(cmsArticle);
            if(attachmentId != null){
                articalAttachmentService.update(cmsArticle.getId(),attachmentId);
            }
        } catch (Exception e) {
            LOGGER.error("修改内容信息 %s:",e);
            throw new ServiceException("修改内容出错",e);
        }

        return num;
    }

    /**
     * 删除内容
     * 根据主键删除
     * @param
     * @return
     */
    @Override
    @Transactional(readOnly = false,rollbackFor = ServiceException.class)
    public int delete(String id) {
        int num=0;
        try {
            int count=articalAttachmentService.delete(id);
            if (count>0) {
                num = cmsArticleMapper.deleteByPrimaryKey(id);
            }
        } catch (Exception e) {
            LOGGER.error("删除内容信息 %s:",e);
            throw new ServiceException("删除内容出错",e);
        }
        return num;
    }
}
